package com.huhu.myharmony.ble;

import com.huhu.myharmony.ble.controller.BleHelper;
import com.huhu.myharmony.ble.slice.BleAbilitySlice;
import com.huhu.myharmony.utils.Toast;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.bluetooth.BluetoothHost;
import ohos.bluetooth.BluetoothRemoteDevice;
import ohos.bundle.IBundleManager;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.eventhandler.InnerEvent;

public class BleAbility extends Ability {
    private BleAbilitySlice bleAbilitySlice;
    // 获取 BluetoothHost 实例，管理本机蓝牙操作
    private BluetoothHost bluetooth = BluetoothHost.getDefaultHost(this);

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setMainRoute(BleAbilitySlice.class.getName());

        if (bluetooth.getBtState() != BluetoothHost.STATE_ON) {
            bluetooth.enableBt();
            myEventHandler.sendEvent(1000);
        } else {
            requestPermissions();
        }

    }

    private void requestPermissions() {
        if (verifySelfPermission("ohos.permission.LOCATION") != IBundleManager.PERMISSION_GRANTED) {
            // 应用未被授予权限
            if (canRequestPermission("ohos.permission.LOCATION")) {
                // 是否可以申请弹框授权(首次申请或者用户未选择禁止且不再提示)
                requestPermissionsFromUser(new String[]{"ohos.permission.LOCATION"}, 100);
            } else {
                // 显示应用需要权限的理由，提示用户进入设置授权
                // 权限被拒绝
                Toast.makeToast(this, "扫描蓝牙需要开启定位权限", Toast.TOAST_SHORT).show();
            }
        } else {
            // 权限已被授予
            myEventHandler.postTask(new Runnable() {
                @Override
                public void run() {
                    bleAbilitySlice.startOrStopScan();
                }
            },1000);
        }

    }

    @Override
    public void onRequestPermissionsFromUserResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case 100:// 匹配requestPermissions的requestCode
                if (grantResults.length > 0 && grantResults[0] == IBundleManager.PERMISSION_GRANTED) {
                    // 权限被授予
                    bleAbilitySlice.startOrStopScan();
                } else {
                    // 权限被拒绝
                    Toast.makeToast(this, "扫描蓝牙需要开启定位权限", Toast.TOAST_SHORT).show();
                }
                break;
            default:
                break;
        }
    }

    private final EventHandler myEventHandler = new EventHandler(EventRunner.getMainEventRunner()) {
        @Override
        protected void processEvent(InnerEvent event) {
            super.processEvent(event);
            switch (event.eventId) {
                case 1000:
                    if (bluetooth.getBtState() != BluetoothHost.STATE_ON) {
                        myEventHandler.sendEvent(1000, 1000);
                    } else {
                        requestPermissions();
                    }
                    break;

                default:
                    break;
            }
        }
    };

    public BleAbilitySlice getBleAbilitySlice() {
        return bleAbilitySlice;
    }

    public void setBleAbilitySlice(BleAbilitySlice bleAbilitySlice) {
        this.bleAbilitySlice = bleAbilitySlice;
    }

    @Override
    protected void onInactive() {
        super.onInactive();
    }

    @Override
    protected void onActive() {
        super.onActive();

    }

    @Override
    protected void onStop() {
        super.onStop();
        BleHelper.getInstance().onDestroy();
    }
}
